package com.hm.cdz.trace;

import com.hm.common.conversion.Ascii;
import com.hm.common.conversion.U16;

/**
 * @author shishun.wang
 * @date 2018年5月7日 下午2:46:24
 * @version 1.0
 * @describe
 */
public class Trance75b {

	public static void main(String[] args) {
		String str = "5353 1F00 75 01088A291B890B28000002010103063030303030306F0100 A5 45";
		str = "8A291B890B280000 02 01010306 3030303030306F0100";
		// String id = str.substring(0, 16);
		// System.out.println("原始报文:53 53 1F 00 75 01 08 8A 29 1B 89 0B 28 00 00 02 01
		// 01 03 06 30 30 30 30 30 30 6F 01 00 A5 45 ");
		// System.out.println("充电桩ID:"+new U16("8A291B890B280000").toU10());
		// System.out.println("充电口编号:"+new U16("02").toU10());
		// System.out.println("localid:"+new U16("01010306").toU10());
		// System.out.println("数字签名:"+new U16(new
		// U16("3030303030306F0100").toU10()+"").toAscii());

		System.out.println(new U16("1F00").toU10());// 165
		System.out.println(new Ascii("CS").toU16());

		str = "0108 8A291B890B280000 0201 01 0306 303030303030 6F0100";

		System.out.println(new U16("08").toU10());// 8
		System.out.println(new U16("8A291B890B280000").toU10());// 2147483647
		System.out.println(new U16("01").toU10());// 1
		// 1
		System.out.println(new U16("06").toU10());// 6
		System.out.println(new U16("303030303030").toU10());// 2147483647
		// 0

		str = "53531F007501088A291B890B28000002010103063030303030306F0100A545";
		byte[] by = toBytes(str);
		System.out.println(str.length() + ";" + by.length);

		// 序号
		System.out.println("序号" + subBytes(by, 0, 2));

	}

	public static byte[] subBytes(byte[] src, int begin, int count) {
		byte[] bs = new byte[count];
		System.arraycopy(src, begin, bs, 0, count);
		return bs;
	}

	/**
	 * 将16进制字符串转换为byte[]
	 * 
	 * @param str
	 * @return
	 */
	public static byte[] toBytes(String str) {
		if (str == null || str.trim().equals("")) {
			return new byte[0];
		}

		byte[] bytes = new byte[str.length() / 2];
		for (int i = 0; i < str.length() / 2; i++) {
			String subStr = str.substring(i * 2, i * 2 + 2);
			bytes[i] = (byte) Integer.parseInt(subStr, 16);
		}

		return bytes;
	}
}
